const workflowStatus = [
  { key: -1, value: "草稿" },
  { key: 0, value: "流转结束" },
  { key: 1, value: "用户取消" },
  { key: 2, value: "管理员取消" },
  { key: 3, value: "流转中" },
  { key: 4, value: "流转中(曾被拒绝过)" },
  { key: 5, value: "未流转" },
  { key: 6, value: "表单失效" },
];

const mixins = {
  computed: {
    CurrentUserId() {
      return this.$store.getters.currentUserId();
    }
  },
  methods: {
    getWorkflowStatusText(status) {
      status = status == undefined ? 3 : status;

      return (
        workflowStatus.find((w) => w.key == status) || {
          text: "未知状态",
        }
      ).value;
    },
    getApi(key, needUserId) {
      key = key || '';

      return key.startsWith('/') ? key : this.$store.getters.getApi(key, needUserId);
    },
    filters(items, keywords, props) {
      if (!items || items.length == 0) {
        return [];
      }

      let datas = items.filter(data => {
        let finded = false;

        for (let prop of props) {
          finded = ((data[prop] || '') + '').toLocaleLowerCase().indexOf(keywords.toLocaleLowerCase()) != -1;

          if (finded) {
            break;
          }
        }

        return finded;
      });

      return datas;
    },
    dateFormat(value, fmt) {
      if (!value) {
        return '';
      }

      fmt = fmt || 'yyyy-MM-dd';
      let date = new Date(value);

      let o = {
        "M+": date.getMonth() + 1,
        "d+": date.getDate(),
        "h+": date.getHours(),
        "m+": date.getMinutes(),
        "s+": date.getSeconds(),
        "q+": Math.floor((date.getMonth() + 3) / 3),
        "S": date.getMilliseconds()
      };

      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
      }

      for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
      }

      return fmt;
    },
    getFormUrl(url, params) {
      url = (url || '').startsWith('~') ? url.substr(1) : url;

      let segments = '';
      let keys = Object.getOwnPropertyNames(params);

      for (let key of keys) {
        segments += `${key}=${params[key]}&`;
      }

      return url.indexOf('?') != -1 ? `${url}&${segments}referer=vant` : `${url}?${segments}referer=vant`;
    }
  },
};

export default {
  install(app) {
    app.mixin(mixins);
  },
};
